package cn.tx.pool;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCDemo1 {

    static  final  String username="root";
    static  final  String password="root";
    static  final  String url="jdbc:mysql://127.0.0.1:3306/mydb4";
    static  final  String driver="com.mysql.jdbc.Driver";

    public static void insert(){
        String sql="insert into emp( EMP_ID, ENAME , GENDER ,HIREDATE) values(null,'zhangsan',1,'1999-12-12');";
        

        Connection conn=null;
        Statement stmt=null;
        try {
            //驱动加载
            Class.forName(driver);
            //建立连接
            conn = DriverManager.getConnection(url, username, password);
            //获得一个执行SQL的对象
            stmt= conn.createStatement();
            //执行SQL
            int i = stmt.executeUpdate(sql);
            System.out.println("插入完毕"+i+"条");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

    public static void insert1(){
        String sql="insert into emp( EMP_ID, ENAME , GENDER ,HIREDATE) values(null,'zhangsan',1,'1999-12-12');";


        Connection conn=null;
        Statement stmt=null;
        try {
            Properties properties = new Properties();
            InputStream in = JDBCDemo1.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(in);

            //驱动加载
            Class.forName(driver);
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

            //建立连接
            conn = dataSource.getConnection();
            //获得一个执行SQL的对象
            stmt= conn.createStatement();
            //执行SQL
            int i = stmt.executeUpdate(sql);
            System.out.println("插入完毕"+i+"条");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(stmt!=null){
                    stmt.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }


    public static void main(String[] args) {

        insert1();
    }
}
